package com.yangkunhong.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class BeanUtils {
    private BeanUtils(){}
    public static <T> Map<String, Object> toMap(T obj) throws IllegalAccessException {
        Map<String, Object> result = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            // 设置为可访问，以便访问私有字段
            if(field.get(obj) == null || ((field.get(obj) instanceof String) && (((String) field.get(obj)).isEmpty())) ){
                continue;
            }
            result.put(field.getName(), field.get(obj));
        }
        return result;
    }
}
